package com.common.UITags;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;



/**
 * 数据窗口
 * @author zhanglong
 *
 */
public class DataGridTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	JspWriter out;
	StringBuffer sBF = new StringBuffer();
	
	private String id;
	private String title;
	//是否需要导出数据
	
	private String realid;
	private List headList;//为了表格的数据导出而设计的
	private String multiSelect ="false";
	private String pagination ="true";//是否分页
	private String dbclick;
	
	@Override
	public int doStartTag() throws JspException {
		try{
			headList = new ArrayList<Map<String,String>>();
			realid = "datagrid_" + getUUID();//真实的id,就是datagrid在后台对应id
			out = pageContext.getOut();
			
			sBF.setLength(0);
			sBF.append(" <table ");
			sBF.append(" _realid=\"" + realid + "\" ");
			sBF.append(" class=\"easyui-datagrid\" ");
			if(!(id==null||"".equals(id))){
				sBF.append(" id = \"" +id+"\" ");
			}
			sBF.append(" style=\"height:100%;width:100%;\"");
			sBF.append("data-options=\"");
			if(!(title==null||"".equals(title))){
				sBF.append("title:'" +title +"',");
			}
			sBF.append("rownumbers:true,");//显示行号
			sBF.append("pagination:"+this.pagination+",");//显示分页栏目
			//sBF.append("striped:true,");//表格条纹话
			sBF.append("checkOnSelect:true,");//点击该行就选中该行
			sBF.append("selectOnCheck:true,");//点击复选框也能选中该行
			if(multiSelect == "true"){
				sBF.append("singleSelect:false,");//多选
			}else{
				sBF.append("singleSelect:true,");//单选
			}
			sBF.append("nowrap:true,");//表格被缩小的时候文字不换行
			sBF.append("fitColumns:true,");//宽度自适应
			sBF.append("pageSize:20,");
			//加载成功后的处理
			sBF.append("onLoadSuccess:function(data){");
			sBF.append("afterDataGridLoad(data);}");
			if(this.dbclick!=null&&!"".equals(this.dbclick)){
				sBF.append(",");
				sBF.append("onDblClickRow:function(rowIndex,rowData){");
				sBF.append(this.dbclick);
				sBF.append("}");
			}
			sBF.append("\"");
			sBF.append(" toolbar=\"#"+realid+"\" ");//toobar
			sBF.append(">");
			sBF.append("<thead>");
			sBF.append("<tr>");
			out.print(sBF.toString());
		}catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		
		return EVAL_BODY_INCLUDE;
	}
	
	
	
	
	//get and set方法
	@Override
	public int doEndTag() throws JspException {
		try{
			sBF.setLength(0);
			sBF.append(" </tr> ");
			sBF.append(" </thead> ");
			sBF.append(" <tbody> ");
			sBF.append(" </tbody> ");
			sBF.append(" </table> ");
			out.print(sBF.toString());
			
		}catch(IOException e){
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		return EVAL_PAGE;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}


	//为了表格的数据导出而设计的
	public List getHeadList() {
		return headList;
	}


	public void setHeadList(List headList) {
		this.headList = headList;
	}




	//其他方法
	public  String getUUID() {
		return java.util.UUID.randomUUID().toString().replace("-", "");
	}




	public String getRealid() {
		return realid;
	}




	public void setRealid(String realid) {
		this.realid = realid;
	}




	public String getMultiSelect() {
		return multiSelect;
	}




	public void setMultiSelect(String multiSelect) {
		this.multiSelect = multiSelect;
	}




	public String getPagination() {
		return pagination;
	}




	public void setPagination(String pagination) {
		this.pagination = pagination;
	}




	public String getDbclick() {
		return dbclick;
	}




	public void setDbclick(String dbclick) {
		this.dbclick = dbclick;
	}

}
